package com.dyz.wsw.domain.admin.controller;

import com.dyz.wsw.utils.IPUtils;
import com.dyz.wsw.common.consts.web.HttpConst;
import com.dyz.wsw.common.web.JsonResult;
import com.dyz.wsw.domain.admin.pojo.LoginInfoDTO;
import com.dyz.wsw.domain.admin.service.IUserService;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;


/**
 * @author dyz
 * Time 2024/6/26 17:15
 */
@RestController
@RequestMapping("/admin/user")
@Validated
public class AdminUserController implements HttpConst {

    private static final Logger log = LoggerFactory.getLogger(AdminUserController.class);
    private final IUserService userService;

    @Autowired
    public AdminUserController(IUserService userService){
        this.userService = userService;
    }

    /**
     * 登录
     * @param loginInfoDTO 用户表单信息
     * @param exchange 本次请求连接
     * @return 登录结果
     */
    @PostMapping("/login")
    public Mono<JsonResult> login(@Valid @RequestBody LoginInfoDTO loginInfoDTO, ServerWebExchange exchange){
        String userAgent  = exchange.getRequest().getHeaders().getFirst(HEADER_USER_AGENT);
        String ip = IPUtils.getIp(exchange);
        return userService.handleLogin(loginInfoDTO,ip,userAgent);
    }

    /**
     * 用户登出
     * @param id 登出id
     * @return 登出结果
     */
    @PostMapping("/logout/{id}")
    public Mono<JsonResult> logout(@PathVariable Long id){
        return userService.handleLogout(id);
    }
}
